//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.jeeplus.common.web;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import com.jeeplus.common.utils.QiNiuUtils;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Client;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

public abstract class UploaderBaseController {
    public UploaderBaseController() {
    }

    @ResponseBody
    @RequestMapping(
        method = {RequestMethod.POST},
        headers = {"content-type=multipart/form-data"}
    )
    public JSONObject upload(@RequestParam("files") CommonsMultipartFile multipartFile) throws IOException {
        return this.doUpload(multipartFile);
    }

    @ResponseBody
    @RequestMapping(
        value = {"/image"},
        method = {RequestMethod.POST},
        headers = {"content-type=multipart/form-data"}
    )
    public JSONObject imageUpload(@RequestParam("image") CommonsMultipartFile multipartFile) throws IOException {
        return this.doUpload(multipartFile);
    }

    @ResponseBody
    @RequestMapping(
        value = {"/video"},
        method = {RequestMethod.POST},
        headers = {"content-type=multipart/form-data"}
    )
    public JSONObject videoUpload(@RequestParam("video") CommonsMultipartFile multipartFile) throws IOException {
        String oFileName = multipartFile.getOriginalFilename();
        String ext = ".tmp";
        if(oFileName.contains(".")) {
            ext = oFileName.substring(oFileName.lastIndexOf("."));
        }

        String baseFileName = UUID.randomUUID().toString();
        String fileName = baseFileName + ext;
        String newFileName = baseFileName + ".mp4";
        JSONObject result = new JSONObject();
        if(!newFileName.equals(fileName)) {
            fileName = "tmp/" + fileName;
            QiNiuUtils.upload(multipartFile.getBytes(), fileName);
            String persistentId = QiNiuUtils.toMp4(fileName, newFileName);
            result.put("persistentId", persistentId);
        } else {
            QiNiuUtils.upload(multipartFile.getBytes(), newFileName);
        }

        result.put("url", QiNiuUtils.DOMAIN + "/" + newFileName);
        return result;
    }

    @ResponseBody
    @RequestMapping(
        value = {"/video/query/status/{persistentId:.+}"},
        method = {RequestMethod.GET}
    )
    public JSONObject queryVideoStatus(@PathVariable("persistentId") String persistentId) throws QiniuException {
        Client client = new Client();
        return JSONObject.fromObject(client.get("http://api.qiniu.com/status/get/prefop?id=" + persistentId).bodyString());
    }

    @ResponseBody
    @RequestMapping(
        value = {"/images"},
        method = {RequestMethod.POST},
        headers = {"content-type=multipart/form-data"}
    )
    public JSONArray batchImageUpload(@RequestParam(value = "images",required = false) List<CommonsMultipartFile> multipartFiles1, @RequestParam(value = "images[]",required = false) List<CommonsMultipartFile> multipartFiles2) throws IOException {
        List<CommonsMultipartFile> multipartFiles = multipartFiles1 == null?multipartFiles2:multipartFiles1;
        JSONArray items = new JSONArray();
        Iterator var6 = multipartFiles.iterator();

        while(var6.hasNext()) {
            CommonsMultipartFile multipartFile = (CommonsMultipartFile)var6.next();
            items.add(this.doUpload(multipartFile));
        }

        return items;
    }

    protected JSONObject doUpload(CommonsMultipartFile multipartFile) throws IOException {
        String oFileName = multipartFile.getOriginalFilename();
        String ext = ".tmp";
        if(oFileName.contains(".")) {
            ext = oFileName.substring(oFileName.lastIndexOf("."));
        }

        String fileName = UUID.randomUUID() + ext;
        QiNiuUtils.upload(multipartFile.getBytes(), fileName);
        JSONObject result = new JSONObject();
        result.put("url", QiNiuUtils.DOMAIN + "/" + fileName);
        return result;
    }
}
